//
//  CustomAlertView.m
//  Cattle
//
//  Created by 刘鵬 on 15/11/9.
//  Copyright © 2015年 xxcao. All rights reserved.
//

#import "AlertView.h"
#import "CommonFunc.h"

@interface AlertView ()
    @property (weak, nonatomic) IBOutlet UILabel *titleLab;
    @property (weak, nonatomic) IBOutlet UILabel *contentLab;
    @property(nonatomic,strong) UIView *bgView;
    @property (weak, nonatomic) IBOutlet UIImageView *line1;
    @property (weak, nonatomic) IBOutlet UIImageView *line2;
    @property (weak, nonatomic) IBOutlet UIButton *btnLeft;
    @property (weak, nonatomic) IBOutlet UIButton *btnRight;
    
    @property (nonatomic,copy) void(^completionHandler)(NSInteger);
@property (nonatomic,assign) BOOL isShow;

    @end

@implementation AlertView

+ (instancetype)mainAlertView {
    static AlertView *singleton = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (!singleton) {
            singleton = [[[self class] alloc] init];
        }
    });
    return singleton;
}

- (BOOL)checkAppServerSion:(NSString *)serversion isClickApp:(BOOL)isClickApp {
    NSArray *serArr = [serversion componentsSeparatedByString:@"."];
    NSArray *nowArr = [AppVersion componentsSeparatedByString:@"."];
    NSLog(@"%@--%@", serversion, AppVersion);
    if(serArr.count >= 2 && nowArr.count >= 2) {
        if(![serArr[0] isEqualToString:nowArr[0]]) {
            [self updateVewsin:YES UpdateInfo:serversion];
            return YES;
        }
        if(![serArr[1] isEqualToString:nowArr[1]]) {
            [self updateVewsin:NO UpdateInfo:serversion];
            return NO;
        }
    }
    if(serArr.count >= 3 && nowArr.count >= 3) {
        if(![serArr[2] isEqualToString:nowArr[2]]) {
            [self updateVewsin:NO UpdateInfo:serversion];
            return NO;
        }
    }
    return NO;
}
    
    
- (void)updateVewsin:(BOOL)isBigVersion UpdateInfo:(NSString *)updateInfo {
    if (!self.isShow) {
        self.isShow = YES;
        NSMutableString *mString = nil;
        if (isBigVersion) {
            mString = [NSMutableString stringWithFormat:@"发现新版本%@，请您立即升级", updateInfo];
        } else {
            mString = [NSMutableString stringWithFormat:@"发现新版本%@，您要升级吗", updateInfo];
        }
        AlertView *customAlerView = LoadFromNib(@"AlertView");
        [customAlerView showWithTitle:@"提示"
                              Content:mString
                              btnLeft:@"忽略"
                             btnRight:@"前往下载"
                    CompletionHandler:^(NSInteger buttonIndex) {
                        if (buttonIndex == 1) {
                            self.isShow = NO;
                            NSString *downloadUrlStr = [NSString stringWithFormat:@"itms-services://?action=download-manifest&url=https%%3A%%2F%%2Fwww.pgyer.com%%2Fapiv1%%2Fapp%%2Fplist%%3FaId%%3D3c28c4111c303d380c4b4b7e53a69897%%26_api_key%%3Df2dc44eeac000e437bc9de7f6844b87b"];
                            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:downloadUrlStr]];
                        } else if(buttonIndex == 0){
                            self.isShow = NO;
                            if(isBigVersion){
                                exit(0);
                            }
                        }
                    }];
    }
    
}
    
    
- (void)awakeFromNib {
    [super awakeFromNib];
    //self.layer.borderColor = [UIColor lightGrayColor].CGColor;
    setViewCorner(self, 8);
    SetFrameByHeight(self.line1.frame, minLineWidth);
    SetFrameByWidth(self.line2.frame, minLineWidth);
}
    
-(void)showWithTitle:(NSString *)title Content:(NSString *)content btnLeft:(NSString *)btnLeftStr btnRight:(NSString *)btnRightStr CompletionHandler:(void (^)(NSInteger buttonIndex))completionHandler {
    self.titleLab.text = title;
    self.contentLab.text = content;
    [self.btnLeft setTitle:btnLeftStr forState:UIControlStateNormal];
    [self.btnRight setTitle:btnRightStr forState:UIControlStateNormal];
    CGRect s = [self.contentLab.text boundingRectWithSize:CGSizeMake(self.contentLab.frame.Width,0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.contentLab.font} context:nil];
    CGFloat height = MIN(s.size.height, 240.0);
    CGFloat extraHeight = height - 45.0;
    SetFrameByHeight(self.contentLab.frame, height);
    SetFrameByHeight(self.frame, 150.0 + extraHeight);
    SetFrameByYPos(self.line1.frame, 110.0 + extraHeight);
    SetFrameByYPos(self.line2.frame, 110.0 + extraHeight);
    SetFrameByYPos(self.btnLeft.frame, 110.0 + extraHeight);
    SetFrameByYPos(self.btnRight.frame, 110.0 + extraHeight);
    if (completionHandler != nil) {
        self.completionHandler = completionHandler;
    }
    self.bgView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.bgView.backgroundColor = [UIColor blackColor];
    self.bgView.alpha = 0.0;
    [SingletonApplication.keyWindow addSubview:self.bgView];
    [SingletonApplication.keyWindow addSubview:self];
    self.center = self.bgView.center;
    self.transform = CGAffineTransformScale(self.transform, 0.01, 0.01);
    [UIView animateWithDuration:0.3 animations:^{
        self.transform = CGAffineTransformIdentity;
        self.bgView.alpha = 0.4;
    }];
}
    
- (void)hide {
    [self removeFromSuperview];
    [self.bgView removeFromSuperview];
}
    
- (IBAction)returnAction:(id)sender {
    UIButton * btn = (UIButton *)sender;
    if (self.completionHandler) {
        self.completionHandler(btn.tag);
    }
    [self hide];
}
    
    /*
     // Only override drawRect: if you perform custom drawing.
     // An empty implementation adversely affects performance during animation.
     - (void)drawRect:(CGRect)rect {
     // Drawing code
     }
     */
    
    @end
